<?php

class LoginAction extends Action{
    
    public function register() {
        $this->display();
    }
    
    public function touser() {
        import('ORG.Util.Verify');
        if(Verify::isEmpty(I('post.email'))==false
           || Verify::isEmpty(I('post.password'))==false
           || Verify::isEmpty(I('post.usertype'))==false){
            redirect('reg.html');
            exit;
        }
        $utype = I('usertype');
        $email = I('email');
        $pwd = md5(I('password'));
        $where['email'] =  array('eq',$email);
        $userRes = M('user')->where($where)->find();
        if(!$userRes){
            $data = array(
                'email'    =>  $email,
                'pwd'      =>  $pwd,
                'ctime'    =>  time(),
            );
            $res = M('user')->add($data);
            if($res){
                //创建用户数据表
                $data = array(
                    'uid' => $res,
                    'utype' => $utype,
                    'nickname' => $email,
                    'teamname' => '',
                    'teamid' => -1,
                    'projectname' => '',
                    'projectid' => -1,
                    'uptime'=>  time(),
                );
                $resUserinfo = M('user_info')->add($data);
                
                //添加account表记录
                $account = array('uid'=>$res);
                M('account')->add($account);
                
                cookie('uid',$res);
                M('user')->where('id='.$res)->setField('identifer', 'logined');//设置已登陆状态
                $this->assign("uid", $res);
                $this->assign('userinfo', $data);
                
                //立即发送激活邮件
                signupEV($email, $res, true);
            }
        }
        else {
            $this->redirect('Login/register');
        }
        
        $this->display();
    }
    
    function signup_checkEmailAjax() {
        $email = I('email');
        $where['email'] = array('eq', $email);
        $userRes = M('user')->where($where)->find();
        if($userRes){
            echo json_encode(FALSE);
        }
        else{
            echo json_encode(TRUE);//echo "";//"该邮箱地址可以使用";
        }
    }
    
}
